package org.benxiong.iplay.util;

import java.util.ArrayList;
import java.util.List;

import org.benxiong.iplay.model.Conversation;
import org.benxiong.iplay.model.Message;
import org.benxiong.iplay.model.PushObject;
import org.benxiong.iplay.model.UserPhoto;
import org.benxiong.iplay.model.UserProfile;
import org.benxiong.iplay.model.UserThumbnail;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;

public class ObjectConverter {

	public static PushObject intentToPushObject(Intent intent) {
		PushObject po = new PushObject();
		return po;
	}

	public static Message pushObjectToMessage(PushObject po) {
		Message msg = new Message();
		return msg;
	}

	public static Message intentToMessage(Intent intent) {
		LogX.print("intent: " + intent.getExtras());
		Message message = new Message();
		message.setFromServer(false);
		message.setSender(intent.getStringExtra("username"));
		message.setText(intent.getStringExtra("text"));
		
		message.setInfo(intent.getStringExtra("info"));
		message.setLatitude(intent.getDoubleExtra("lat", Double.MIN_VALUE));
		message.setLongitude(intent.getDoubleExtra("lng", Double.MIN_VALUE));
		try {
			long timestamp = Long.valueOf(intent.getStringExtra("ts"));
			message.setTime(timestamp);
			int type = Integer.valueOf(intent.getStringExtra("type"));
			message.setType(type);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return message;
	}

	public static Message jsonToMessage(JSONObject jo) {
		Message message = new Message();
		message.setFromServer(true);
		try {
			message.setText(jo.getString("text"));
			message.setTime(jo.getLong("ts"));
			message.setLatitude(jo.getDouble("lat"));
			message.setLongitude(jo.getDouble("lng"));
			message.setType(jo.getInt("t"));
			message.setMine(jo.getInt("m"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return message;
	}

	public static Conversation jsonToConversation(JSONObject jo) {
		Conversation c = new Conversation();
		c.setFromServer(true);
		try {
			c.setSender(jo.getString("s"));
			c.setTime(jo.getLong("lu"));
			c.setContent(jo.getString("c"));
			c.setMessageType(jo.getInt("t"));
			c.setReadStatus(jo.getInt("ru"));
			c.setReadStatusSender(jo.getInt("su"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}

	public static UserThumbnail jsonToUserThumbnail(JSONObject jo) {
		UserThumbnail t = new UserThumbnail();
		try {
			t.setUsername(jo.getString("u"));
			t.setProfilePictureVersion(jo.getLong("pv"));
			if (!jo.isNull("lat"))
				t.setLatitude(jo.getDouble("lat"));
			if (!jo.isNull("lng"))
				t.setLongitude(jo.getDouble("lng"));
			if (!jo.isNull("t"))
				t.setLastUpdate(jo.getLong("t"));
			if (!jo.isNull("info"))
				t.setInfo(jo.getString("info"));
			// pre calculate
			t.getDistanceToMe();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}

	public static UserPhoto jsonToUserPhoto(JSONObject jo) {
		UserPhoto p = new UserPhoto();
		try {
			p.setId(jo.getString("idString"));
			p.setAccess(jo.getInt("access"));
			p.setUsername(jo.getString("owner"));
			p.setSecretKey(jo.getString("secretKey"));
			p.setSeed(jo.getString("seed"));
			if (!jo.isNull("cc"))
				p.setCountry(jo.getString("cc"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;
	}

	public static UserProfile jsonToUserProfile(JSONObject json) throws JSONException {
		JSONObject jo = json.getJSONObject("data");

		UserProfile p = new UserProfile();
		try {
			if (!jo.isNull("dobString"))
				p.setDobString(jo.getString("dobString"));
			if (!jo.isNull("height"))
				p.setHeight(jo.getInt("height"));
			if (!jo.isNull("weight"))
				p.setWeight(jo.getInt("weight"));
			if (!jo.isNull("heightInch"))
				p.setHeightInch(jo.getInt("heightInch"));
			if (!jo.isNull("weightPounds"))
				p.setWeightPounds(jo.getInt("weightPounds"));
			if (!jo.isNull("measure"))
				p.setMeasure(jo.getInt("measure"));

			p.setRoleIndex(jo.getInt("roleIndex"));
			p.setRelationshipIndex(jo.getInt("relationshipIndex"));
			p.setBuildIndex(jo.getInt("buildIndex"));
			p.setEthnicityIndex(jo.getInt("ethnicityIndex"));

			if (!jo.isNull("facebook"))
				p.setFacebook(jo.getString("facebook"));
			if (!jo.isNull("twitter"))
				p.setTwitter(jo.getString("twitter"));
			if (!jo.isNull("weibo"))
				p.setWeibo(jo.getString("weibo"));
			if (!jo.isNull("wechat"))
				p.setWechat(jo.getString("wechat"));
			if (!jo.isNull("instagram"))
				p.setInstagram(jo.getString("instagram"));
			if (!jo.isNull("line"))
				p.setLine(jo.getString("line"));
			
			if (!jo.isNull("kik"))
				p.setKik(jo.getString("kik"));
			
			if (!jo.isNull("nickName"))
				p.setNickName(jo.getString("nickName"));

			if (!jo.isNull("status"))
				p.setStatus(jo.getString("status"));
			if (!jo.isNull("intro"))
				p.setIntro(jo.getString("intro"));
			if (!jo.isNull("privateIntro"))
				p.setPrivateIntro(jo.getString("privateIntro"));
			if (!jo.isNull("myType"))
				p.setMyType(jo.getString("myType"));

			if (!jo.isNull("unlocked"))
				p.setUnlocked(jo.getBoolean("unlocked"));
			if (!jo.isNull("favorited"))
				p.setFavorited(jo.getBoolean("favorited"));
			if (!jo.isNull("blocked"))
				p.setBlocked(jo.getBoolean("blocked"));
			if (!jo.isNull("meetup"))
				p.setMeetup(jo.getBoolean("meetup"));

			// ... looking for
			JSONArray lookingForArray = jo.getJSONArray("lookingFor");
			for (int i = 0; i < lookingForArray.length(); i++) {
				p.getLookingFor().add(lookingForArray.getInt(i));
			}

			// ... lang
			JSONArray langArray = jo.getJSONArray("lang");
			for (int i = 0; i < langArray.length(); i++) {
				p.getLang().add(langArray.getInt(i));
			}

			// set user photos
			if (!json.isNull("extra")) {
				List<UserPhoto> photos = new ArrayList<UserPhoto>();
				JSONArray array = json.getJSONArray("extra");
				for (int i = 0; i < array.length(); i++) {
					photos.add(jsonToUserPhoto(array.getJSONObject(i)));
				}
				p.addPhotos(photos);
			}

			p.setAvatarVersion(jo.getLong("av"));
			p.setCoverVersion(jo.getLong("cv"));
			p.setProfileVersion(jo.getLong("v"));

		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return p;
	}

	public static String indexesToString(List<Integer> indexes, String[] stringArray) {
		if (indexes.size() > 0) {
			try {
				String str = "";
				for (int i = 0; i < indexes.size(); i++) {
					int item = indexes.get(i);
					str += stringArray[item] + ", ";
				}

				if (str.length() > 0) {
					str = str.substring(0, str.length() - 1);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static String indexToStringIgnoreZero(int index, String[] stringArray) {
		if (index == 0 || index + 1 > stringArray.length)
			return null;
		return stringArray[index];
	}
}
